package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.*;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketKeywordMapper;
import com.market.service.admin.AdminKeywordService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author fan
 * @Description:
 * @since 2022/12/30 21:37
 */
@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {
    @Autowired
    MarketKeywordMapper keywordMapper;

    @Override
    public CommonData<MarketKeyword> list(BasePageInfo info, String keyword, String url) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketKeywordExample example = new MarketKeywordExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (!StringUtil.isEmpty(url)) {
            criteria.andUrlLike("%" + url + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> list = keywordMapper.selectByExample(example);
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketKeyword create(MarketKeyword keyword) {
        Date date = new Date();
        keyword.setAddTime(date);
        keyword.setUpdateTime(date);
        keywordMapper.insertSelective(keyword);
        return keyword;
    }

    @Override
    public MarketKeyword update(MarketKeyword keyword) {
        Date date = new Date();
        keyword.setUpdateTime(date);
        keywordMapper.updateByPrimaryKeySelective(keyword);
        return keyword;
    }

    @Override
    public void delete(MarketKeyword keyword) {
        keyword.setDeleted(true);
        keywordMapper.updateByPrimaryKeySelective(keyword);
    }

}
